﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace comdiinfo
{
    public class G64Track
    {
        public int no { get; set; }
        public G64TrackOffset g64TrackOffset { get; set; }
        public G64TrackSize g64TrackSize { get; set; }
        public G64TrackSpeed g64TrackSpeed { get; set; }
        public G64TrackSpeedOffset g64TrackSpeedOffset { get; set; }
        public G64TrackGCRData g64TrackGCRData { get; set; }
        public G64TrackSpaceAfterGCRData g64TrackSpaceAfterGCRData { get; set; }
        public G64TrackSpeedZoneData g64TrackSpeedZoneData { get; set; }
        public void LoadData(ref G64ImageData imgData, int trackNo, G64NumberOfTracks g64NumberOfTracks, G64SizeOfTracks g64SizeOfTracks)
        {
            this.g64TrackOffset = new G64TrackOffset(trackNo);
            this.g64TrackSize = new G64TrackSize(trackNo);
            this.g64TrackSpeed = new G64TrackSpeed(trackNo);
            this.g64TrackSpeedOffset = new G64TrackSpeedOffset(trackNo);
            this.g64TrackGCRData = new G64TrackGCRData(trackNo);
            this.g64TrackSpaceAfterGCRData = new G64TrackSpaceAfterGCRData(trackNo);
            this.g64TrackSpeedZoneData = new G64TrackSpeedZoneData(trackNo);

            this.no = trackNo;
            this.g64TrackOffset.LoadData(ref imgData, ((trackNo - 1) * 4) + 12, 4);
            if (g64TrackOffset.ElementValue > 0)
            {
                this.g64TrackSize.LoadData(ref imgData, g64TrackOffset.ElementValue, 2);

                this.g64TrackGCRData.LoadData(ref imgData, g64TrackOffset.ElementValue + 2, g64TrackSize.ElementValue);
                if (g64SizeOfTracks.ElementValue >= g64TrackSize.ElementValue) // Das ist ein Fehler in der Stucktur des Image!
                {
                    this.g64TrackSpaceAfterGCRData.LoadData(ref imgData, g64TrackOffset.ElementValue + 2 + g64TrackSize.ElementValue, g64SizeOfTracks.ElementValue - g64TrackSize.ElementValue);
                }
                else 
                {
                    g64TrackSize.AddExtraError(String.Format("darf nicht größer sein als {0} ist aber {1}", g64SizeOfTracks.ElementValue, g64TrackSize.ElementValue));
                }
            }
            this.g64TrackSpeed.LoadData(ref imgData, ((trackNo + g64NumberOfTracks.ElementValue - 1) * 4) + 12, 4);
            if (g64TrackSpeed.ElementValue > 3)
            {
                this.g64TrackSpeedOffset.ElementValue = (Int32)g64TrackSpeed.ElementValue;
                this.g64TrackSpeed.ElementValue = 0;
                if (g64TrackSize.ElementValue > 0)
                {
                    int zone_len = (int)((g64TrackSize.ElementValue + 3) / 4);
                    this.g64TrackSpeedZoneData.LoadData(ref imgData, g64TrackSpeedOffset.ElementValue, zone_len);
                }
            }
        }
    }
}
